package com.xtoad.ecms.baseinfo.converter;

import com.xtoad.ecms.baseinfo.dto.MenuDTO;
import com.xtoad.ecms.baseinfo.model.Menu;
import com.xtoad.ecms.common.web.base.IConverter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * menu转换器实现类
 *
 * @author xtoad
 * @date 2020/05/29
 */
@Component
public class MenuConverter implements IConverter<MenuDTO, Menu> {

    /**
     * Model To DTO
     *
     * @param model 源
     * @return 目标
     */
    @Override
    public MenuDTO toDto(Menu model) {
        MenuDTO dto = new MenuDTO();
        dto.setId(model.getId());
        dto.setCreateTime(model.getCreateTime());
        dto.setCreateUser(model.getCreateUser());
        dto.setLastUpdateTime(model.getLastUpdateTime());
        dto.setLastUpdateUser(model.getLastUpdateUser());
        if (model.getParent() != null) {
            MenuDTO parent = new MenuDTO();
            parent.setId(model.getParent().getId());
            parent.setTitle(model.getParent().getTitle());
            parent.setType(model.getParent().getType());
            parent.setCreateTime(model.getParent().getCreateTime());
            parent.setCreateUser(model.getParent().getCreateUser());
            parent.setLastUpdateTime(model.getParent().getLastUpdateTime());
            parent.setLastUpdateUser(model.getParent().getLastUpdateUser());
            dto.setParent(parent);
        }
        dto.setTitle(model.getTitle());
        dto.setType(model.getType());
        return dto;
    }

    /**
     * DTO To Model
     *
     * @param dto 源
     * @return 目标
     */
    @Override
    public Menu toModel(MenuDTO dto) {
        Menu model = new Menu();
        model.setId(dto.getId());
        model.setCreateTime(dto.getCreateTime());
        model.setCreateUser(dto.getCreateUser());
        model.setLastUpdateTime(dto.getLastUpdateTime());
        model.setLastUpdateUser(dto.getLastUpdateUser());
        if (dto.getParent() != null) {
            Menu parent = new Menu();
            parent.setId(dto.getParent().getId());
            parent.setTitle(dto.getParent().getTitle());
            parent.setType(dto.getParent().getType());
            parent.setCreateTime(dto.getParent().getCreateTime());
            parent.setCreateUser(dto.getParent().getCreateUser());
            parent.setLastUpdateTime(dto.getParent().getLastUpdateTime());
            parent.setLastUpdateUser(dto.getParent().getLastUpdateUser());
            model.setParent(parent);
        }
        model.setTitle(dto.getTitle());
        model.setType(dto.getType());
        return model;
    }

    /**
     * 批量Model To DTO
     *
     * @param models 源
     * @return 目标
     */
    @Override
    public List<MenuDTO> toDtoList(List<Menu> models) {
        List<MenuDTO> dtoList = new ArrayList<>();
        models.forEach(model -> dtoList.add(this.toDto(model)));
        return dtoList;
    }

    /**
     * 批量DTO To Model
     *
     * @param dtos 源
     * @return 目标
     */
    @Override
    public List<Menu> toModelList(List<MenuDTO> dtos) {
        List<Menu> modelList = new ArrayList<>();
        dtos.forEach(dto -> modelList.add(this.toModel(dto)));
        return modelList;
    }
}
